//
//  LPClearTheCache.m
//  LovingPartyBuilding
//
//  Created by 刘应平 on 17/5/19.
//  Copyright © 2017年 刘应平. All rights reserved.
//

#import "LPClearTheCache.h"

@implementation LPClearTheCache
/**  获取本地缓存  */
+ (float)getFilePath{
    //文件管理
    NSFileManager *fileManager = [NSFileManager defaultManager];
    //缓存路径
    NSArray *cachePaths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory,NSUserDomainMask, YES);
    
    NSString *cacheDir = [cachePaths objectAtIndex:0];
    
    NSArray *cacheFileList;
    
    NSEnumerator *cacheEnumerator;
    
    NSString *cacheFilePath;
    
    unsigned long long cacheFolderSize = 0;
    
    cacheFileList = [fileManager subpathsOfDirectoryAtPath:cacheDir error:nil];
    
    cacheEnumerator = [cacheFileList objectEnumerator];
    
    while (cacheFilePath = [cacheEnumerator nextObject]) {
        
        NSDictionary *cacheFileAttributes = [fileManager attributesOfItemAtPath:[cacheDir stringByAppendingPathComponent:cacheFilePath] error:nil];
        
        cacheFolderSize += [cacheFileAttributes fileSize];
        
    }
    //单位MB
    return cacheFolderSize/1024.0/1024.0;
}


/**  清除本地缓存  */
+ (void)clearTheCache {
    
    //点击了确定,遍历整个caches文件,将里面的缓存清空
    NSString *path = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) lastObject];
    NSFileManager *fileManager=[NSFileManager defaultManager];
    if ([fileManager fileExistsAtPath:path]) {
        NSArray *childerFiles=[fileManager subpathsAtPath:path];
        for (NSString *fileName in childerFiles) {
            //如有需要，加入条件，过滤掉不想删除的文件
            NSString *absolutePath=[path stringByAppendingPathComponent:fileName];
            [fileManager removeItemAtPath:absolutePath error:nil];
        }
    }
    
}
@end
